#include "mutex.hpp"
#include "cond.hpp"

int num = 0;
Mutex mutex;
Cond cond;
void* start_routine1(void* arg)
{
    //当num小于等于5时打印
    LockGuard lg(mutex);
    while(num <= 5)
    {
        std::cout << num++ << ' ';
    }
    std::cout << std::endl;
    if(num > 5)
        cond.Signal();//条件满足时再通知
    return nullptr;
}

void* start_routine2(void* arg)
{
    //当num大于5时打印
    LockGuard lg(mutex);
    while(num <= 5)
        cond.Wait(mutex.GetMutex());
    while(num <= 11)
        std::cout << num++ << ' ';
    std::cout << std::endl;
    return nullptr;
}
int main()
{
    std::vector<pthread_t> arr(2);
    pthread_create(&arr[0],nullptr,start_routine1,nullptr);
    pthread_create(&arr[1],nullptr,start_routine2,nullptr);
    for(auto& e : arr)
        pthread_join(e,nullptr);
    return 0;
}